package com.cellulargames.model;

import javax.microedition.lcdui.Image;

import com.cellulargames.control.Painter;
import com.cellulargames.view.HandsPainter;

public class Hand {

	public int xHand;
	
	public int yHand;
	
	private int xHandOrig;
	
	private int yHandOrig;
	
	private int yDescanzo;
	
	private int ySenalar;
	
	public int state;
	
	public static final int DESCANZANDO = 0;
	
	public static final int SELECCIONANDO = 1;
	
	public static final int SENALANDO = 3;
	
	public static final int MANO_DERECHA = 4;
	
	public static final int MANO_IZQUIERDA = 5;
	
	private int orientacion;
	
	private int speed = 10;
	
	public Hand(int state, int orientacion){
		
		this.state = state;
		this.orientacion = orientacion;
		
		if( orientacion == MANO_DERECHA ){
			xHandOrig = Painter.getInstance().getWidth()-HandsPainter.hands[0].getWidth();
			yHandOrig = Painter.getInstance().getHeight()-(HandsPainter.hands[0].getHeight()/2)-10;
		}
		else{
			xHandOrig = 2;
			yHandOrig = Painter.getInstance().getHeight()-(HandsPainter.hands[0].getHeight()/2)-10;
		}
		
		yDescanzo = Painter.getInstance().getHeight()-(HandsPainter.hands[1].getHeight()/4);
		
		ySenalar = 215;
		
		xHand = xHandOrig;
		yHand = yHandOrig;
		
	}
	
	public Image getHand(){
		switch( state ){
		case SELECCIONANDO:
			if( orientacion == MANO_DERECHA ){
				return HandsPainter.hands[0];
			}
			else{
				return HandsPainter.hands[1];
			}
			
			
		case DESCANZANDO:
			if( orientacion == MANO_DERECHA ){
				return HandsPainter.hands[0];
			}
			else{
				return HandsPainter.hands[1];
			}
			
		case SENALANDO:
			if( orientacion == MANO_DERECHA ){
				return HandsPainter.hands[2];
			}
			else{
				return HandsPainter.hands[3];
			}
		}
		return null;
	}
	
	public void press() {
		yHand += 2;
	}
	
	public void release() {
		yHand -= 2;
	}
	
	public void senala(int x){
		
		if( state == SENALANDO ){
			
			if( yHand > ySenalar ){
				if( yHand-speed < ySenalar ){
					yHand = ySenalar;
				}
				else{
					yHand-=speed;
				}
			}			
			
			if( xHand < x ){
				if( xHand+speed > x ){
					xHand = x;
				}
				else{
					xHand+=speed;
				}
			}
			else if( xHand > x ){
				if( xHand+speed < x ){
					xHand = x;
				}
				else{
					xHand-=speed;
				}
			}
			
		}//
		
	}
	
	public void descanza(){
		
		if( state == DESCANZANDO ){
			
			if( xHand < xHandOrig ){
				if( xHand+speed > xHandOrig ){
					xHand = xHandOrig;
				}
				else{
					xHand += speed;
				}
			}
			else if( xHand > xHandOrig ){
				if( xHand-speed < xHandOrig ){
					xHand = xHandOrig;
				}
				else{
					xHand -= speed;
				}				
			}
			
			if( yHand < yDescanzo ){
				yHand += speed;
			}
			
		}
		
	}
	
	public void selecciona(int x){
		
		if( state == SELECCIONANDO ){
			
			if( xHand < x ){
				if( xHand-speed > x ){
					xHand = x;
				}
				else{
					xHand += speed;
				}
			}
			else if( xHand > x ){
				if( xHand-speed < x ){
					xHand = x;
				}
				else{
					xHand -= speed;
				}
			}
			
			if( yHand > yHandOrig ){
				if( yHand-speed < yHandOrig ){
					yHand = yHandOrig;
				}
				else{
					yHand -= speed;
				}
			}						
			else if( yHand < yHandOrig ){
				if( yHand+speed > yHandOrig ){
					yHand = yHandOrig;
				}
				else{
					yHand += speed;
				}
			}
			
		}
		
	}
	
	
}
